#pragma once
#include <QSqlDatabase>
#include <QMap>
#include <QString>
#include <QList>
#include <QVariantMap>
#include "ManagerBase.h"
#include "HistoryHelper.h"
#include "User.h"
#include "Invitation.h"

class DbManager : public ManagerBase<DbManager> {
public:
    enum STATUS {
        SUCCESS_LOGIN,
        SUCCESS_REG,
        NO_USER,
        WRONG_PASS,
        ERROR
    };

    inline static const QMap<STATUS, QByteArray> statusMap {
        {SUCCESS_LOGIN, "SUCCESS_LOGIN"},
        {SUCCESS_REG, "SUCCESS_REG"},
        {NO_USER, "NO_USER"},
        {WRONG_PASS, "WRONG_PASS"},
        {ERROR, "ERROR"}
    };
    static QByteArray getStatusString(STATUS status) { return statusMap[status]; }

    void init(const QString& path);
    bool addInvitation(const quint16& roomId,const quint16& hostId, const quint16& userId);
    void delInvitation(const quint16& id);
    bool addMeeting(const quint16& roomId, const QString& time);

    User queryUser(const quint16& userId);
    QList<History> queryUserHistoryWorkOrders(quint16 userId);
    QList<User> queryAllUsersAlphabetical();
    QList<User> queryAllUsersAlphabetical(quint16 userType);
    QList<Invitation> queryAllInvitations(const quint16& userId);
    STATUS checkUser(const QString& username, const QString& pass, quint16& outId);
    STATUS checkUser(const quint32& phone, const QString& pass, quint16& outId);
    STATUS registerUser(const QString& username, const QString& pass, const quint32& phone, quint16 userType);
private:
    QSqlDatabase m_db;
};

using STATUS = DbManager::STATUS;